//
//  HKUserInfoViewController.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/10.
//

#import "HKUserInfoViewController.h"
#import "HKUser.h"
#import "HKTableAdapter.h"
#import "HKUserInfoCell.h"
#import "HKUserInfoHeaderView.h"
#import "UIView+properties.h"
#import <JPUSHService.h>

@interface HKUserInfoViewController ()

@property (nonatomic, strong) UIButton *logoutButton;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HKTableAdapter *tableAdapter;

@end

@implementation HKUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.userDefinedNaviBar updateTitle:@"用户信息"];
    [self.view addSubview:self.tableView];
    [self setupSectionModels];
    CGFloat naviBarH = self.userDefinedNaviBar.height;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(naviBarH, 0, 0, 0));
    }];
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HKScreenWidth, 80)];
    [footer addSubview:self.logoutButton];
    self.tableView.tableFooterView = footer;
}

- (UIButton *)logoutButton {
    if (!_logoutButton) {
        _logoutButton = [UIButton new];
        _logoutButton.frame = CGRectMake(40, 20, HKScreenWidth-80, 40);
        [_logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
        _logoutButton.layer.cornerRadius = 20;
        _logoutButton.clipsToBounds = YES;
        _logoutButton.backgroundColor = UIColorFromRGB(0x4549FF);
        [_logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    }
    return _logoutButton;
}

- (void)logout{
    UIAlertController *alert = [UIAlertController
                                
                                alertControllerWithTitle:nil message:nil
                                
                                preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消"   style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        
    }];
    UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"退出登录"   style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        HKUser *user = WMglobalUser();
        if (user) {
            [JPUSHService deleteAlias:^(NSInteger iResCode, NSString * _Nullable iAlias, NSInteger seq) {
                if (iResCode == 6002 || iResCode == 6014 || iResCode == 6020 || iResCode == 6021 || iResCode == 6022) {
                    NSLog(@"清除别名 code：%ld",(long)iResCode);
                } else if(iResCode == 0){
                    NSLog(@"清除别名成功");
                }else{
                    NSLog(@"清除别名 失败code：%ld",(long)iResCode);
                }
            } seq:0];
        }
        logout();
    }];
//    UIAlertAction *unregisterButton = [UIAlertAction actionWithTitle:@"注销用户"   style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//        logout();
//    }];
    [alert addAction:logOut];
    //[alert addAction:unregisterButton];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}


- (void)setupSectionModels {
    HKUser *user = WMglobalUser();
    
    HKListSectionModel *sectionModel = [HKListSectionModel new];
    
    HKSectionHeaderFooterViewModel *headerViewModel = [self createHeaderViewModelWithImageName:@"baseInfoIcon" title:@"基本信息"];
    sectionModel.headerViewModel = headerViewModel;
    
    HKListCellModel *cellModel1 = [self createCellModelWithTitle:@"姓名" info:user.name];
    [sectionModel addCellModel:cellModel1];
    
    HKListCellModel *cellModel2 = [self createCellModelWithTitle:@"年龄" info:@(user.age).stringValue];
    [sectionModel addCellModel:cellModel2];
    
    NSString *sexStr;
    if (user.sex == 1) {
        sexStr = @"男";
    } else if (user.sex == 2) {
        sexStr = @"女";
    }
    HKListCellModel *cellModel3 = [self createCellModelWithTitle:@"性别" info:sexStr];
    [sectionModel addCellModel:cellModel3];
    
    HKListCellModel *cellModel4 = [self createCellModelWithTitle:@"出生日期" info:user.birthday];
    [sectionModel addCellModel:cellModel4];
    
    HKListCellModel *cellModel5 = [self createCellModelWithTitle:@"教育年限" info:@(user.eduYears).stringValue];
    [sectionModel addCellModel:cellModel5];
    
    HKListCellModel *cellModel6 = [self createCellModelWithTitle:@"邮箱" info:user.email?:@"未填写"];
    [sectionModel addCellModel:cellModel6];
    
    HKListCellModel *cellModel7 = [self createCellModelWithTitle:@"手机号" info:user.phone?:@"未填写"];
    [sectionModel addCellModel:cellModel7];
    
    HKListSectionModel *sectionModel1 = [HKListSectionModel new];
    
    HKSectionHeaderFooterViewModel *headerViewModel1 = [self createHeaderViewModelWithImageName:@"baseInfoIcon" title:@"实验信息"];
    sectionModel1.headerViewModel = headerViewModel1;
    
    HKListCellModel *cellModel11 = [self createCellModelWithTitle:@"用户号" info:user.number];
    [sectionModel1 addCellModel:cellModel11];
    
    HKListCellModel *cellModel12 = [self createCellModelWithTitle:@"当前主试姓名" info:user.mainTesterName];
    [sectionModel1 addCellModel:cellModel12];
    
    HKListCellModel *cellModel13 = [self createCellModelWithTitle:@"当前主试联系方式" info:user.mainTesterPhone];
    [sectionModel1 addCellModel:cellModel13];
    
    [self.tableAdapter addSectionModels:@[sectionModel,sectionModel1]];
    [self.tableAdapter reloadData];
 
}

- (HKSectionHeaderFooterViewModel *)createHeaderViewModelWithImageName:(NSString *)imageName title:(NSString *)title {
    HKSectionHeaderFooterViewModel *headerViewModel = [HKSectionHeaderFooterViewModel new];
    HKUserInfoHeaderViewViewModel *viewModel = [HKUserInfoHeaderViewViewModel new];
    viewModel.imageName = imageName;
    viewModel.title = title;
    headerViewModel.content = viewModel;
    headerViewModel.height = [HKUserInfoHeaderView cellHeightWithContent:viewModel];
    headerViewModel.width = self.view.width;
    headerViewModel.viewClass = HKUserInfoHeaderView.class;
    [headerViewModel setRender:^(__kindof HKSectionHeaderFooterViewModel * _Nonnull headerFooterItem, id  _Nonnull view) {
        HKUserInfoHeaderView *tempView = KSSAFE_CAST(view, HKUserInfoHeaderView);
        [tempView updateContent:headerFooterItem.content];
    }];
    return headerViewModel;
}

- (HKListCellModel *)createCellModelWithTitle:(NSString *)title info:(NSString *)userInfo {
    HKListCellModel *cellModel = [HKListCellModel new];
    cellModel.cellClass = HKUserInfoCell.class;
    HKUserInfoCellViewModel *cellViewModel = [HKUserInfoCellViewModel new];
    cellViewModel.title = title;
    cellViewModel.userInfo = userInfo;
    cellModel.content = cellViewModel;
    cellModel.cellHeight = [HKUserInfoCell cellHeightWithContent:cellViewModel];
    [cellModel setRender:^(__kindof HKListCellModel * _Nonnull cellModel, id  _Nonnull cell) {
        HKBaseTableViewCell *tbCell = KSSAFE_CAST(cell, HKBaseTableViewCell);
        [tbCell updateContent:cellModel.content];
    }];
    return cellModel;
}

#pragma mark - getter and setter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [HKTableAdapter createTableViewWithFrame:self.view.bounds];
    }
    return _tableView;
}

- (HKTableAdapter *)tableAdapter {
    if (!_tableAdapter) {
        _tableAdapter = [[HKTableAdapter alloc] initWithTableView:self.tableView];
    }
    return _tableAdapter;
}

@end
